﻿Imports System
Imports System.CodeDom
Imports System.Collections
Imports System.ComponentModel.Design.Serialization
Imports System.IO
Imports System.Text
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Xml

Namespace System.Workflow.ComponentModel.Serialization
    <DefaultSerializationProvider(GetType(ActivityMarkupSerializationProvider))> _
    Public Class ActivityMarkupSerializer
        Inherits WorkflowMarkupSerializer
        ' Methods
        Protected Overrides Function CreateInstance(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal type As Type) As Object
            Dim reader As XmlReader = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(XmlReader)),XmlReader)
            If (reader Is Nothing) Then
                Return Nothing
            End If
            Dim obj2 As Object = MyBase.CreateInstance(serializationManager, type)
            If ((TypeOf obj2 Is Activity AndAlso (serializationManager.Context.Item(GetType(Activity)) Is Nothing)) AndAlso (Not serializationManager.Context.Item(GetType(WorkflowCompilerParameters)) Is Nothing)) Then
                [TryCast](obj2,Activity).UserData.Item(UserDataKeys.CustomActivity) = False
            End If
            Dim customAttributes As WorkflowMarkupSourceAttribute() = DirectCast(type.GetCustomAttributes(GetType(WorkflowMarkupSourceAttribute), False), WorkflowMarkupSourceAttribute())
            If ((TypeOf obj2 Is CompositeActivity AndAlso (customAttributes.Length > 0)) AndAlso (type.Assembly Is serializationManager.LocalAssembly)) Then
                Dim obj3 As Object = Nothing
                [Using] reader2 As XmlReader = XmlReader.Create(customAttributes(0).FileName)
                    obj3 = MyBase.Deserialize(serializationManager, reader2)
                End [Using]
                ActivityMarkupSerializer.ReplaceChildActivities([TryCast](obj2,CompositeActivity), [TryCast](obj3,CompositeActivity))
            End If
            If TypeOf obj2 Is Activity Then
                Dim num6 As Integer
                Dim num As Integer = If(TypeOf reader Is IXmlLineInfo, DirectCast(reader, IXmlLineInfo).LineNumber, 1)
                Dim num2 As Integer = If(TypeOf reader Is IXmlLineInfo, DirectCast(reader, IXmlLineInfo).LinePosition, 1)
                Dim num3 As Integer = (num - 1)
                Dim num4 As Integer = (num2 - 1)
                Dim flag As Boolean = False
                Do While reader.MoveToNextAttribute
                    flag = True
                Loop
                Dim num5 As Integer = (num - 1)
                If flag Then
                    reader.ReadAttributeValue
                    num6 = (num2 + reader.Value.Length)
                Else
                    num6 = ((num2 + reader.Name.Length) - 1)
                End If
                reader.MoveToElement
                Dim activity As Activity = DirectCast(obj2, Activity)
                activity.SetValue(ActivityMarkupSerializer.StartLineProperty, num3)
                activity.SetValue(ActivityMarkupSerializer.StartColumnProperty, num4)
                activity.SetValue(ActivityMarkupSerializer.EndLineProperty, num5)
                activity.SetValue(ActivityMarkupSerializer.EndColumnProperty, num6)
            End If
            Return obj2
        End Function

        Protected Overrides Sub OnAfterSerialize(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim activity As Activity = [TryCast](obj,Activity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(Activity).FullName }), "obj")
            End If
            Dim writer As XmlWriter = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(XmlWriter)),XmlWriter)
            If (Not writer Is Nothing) Then
                Dim writer2 As StringWriter = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(StringWriter)),StringWriter)
                If (Not writer2 Is Nothing) Then
                    Dim str As String = writer2.ToString
                    Dim startIndex As Integer = CInt(activity.GetValue(ActivityMarkupSerializer.EndColumnProperty))
                    If (str.IndexOf(writer2.NewLine, startIndex) = -1) Then
                        activity.SetValue(ActivityMarkupSerializer.EndColumnProperty, ((str.Length - startIndex) - 1))
                    Else
                        activity.SetValue(ActivityMarkupSerializer.EndColumnProperty, (str.IndexOf(writer2.NewLine, startIndex) - startIndex))
                    End If
                End If
                Dim members As CodeTypeMemberCollection = [TryCast](activity.GetValue(WorkflowMarkupSerializer.XCodeProperty),CodeTypeMemberCollection)
                If (Not members Is Nothing) Then
                    Dim member As CodeSnippetTypeMember
                    For Each member In members
                        If (member.Text Is Nothing) Then
                            [Continue] For
                        End If
                        writer.WriteStartElement("x", "Code", "http://schemas.microsoft.com/winfx/2006/xaml")
                        Dim writerDepth As Integer = serializationManager.WriterDepth
                        Dim builder As New StringBuilder
                        If member.UserData.Contains(UserDataKeys.CodeSegment_New) Then
                            builder.AppendLine
                            Dim str2 As String
                            For Each str2 In member.Text.Trim.Split(New String() { ChrW(13) & ChrW(10) }, StringSplitOptions.None)
                                builder.Append(writer.Settings.IndentChars)
                                builder.Append(str2)
                                builder.AppendLine
                            Next
                            builder.Append(writer.Settings.IndentChars)
                        Else
                            builder.Append(member.Text)
                        End If
                        writer.WriteCData(builder.ToString)
                        writer.WriteEndElement
                    Next
                End If
            End If
        End Sub

        Protected Overrides Sub OnBeforeSerialize(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim activity As Activity = [TryCast](obj,Activity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(Activity).FullName }), "obj")
            End If
            Dim writer As XmlWriter = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(XmlWriter)),XmlWriter)
            If (Not writer Is Nothing) Then
                Dim writer2 As StringWriter = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(StringWriter)),StringWriter)
                If (Not writer2 Is Nothing) Then
                    writer.Flush
                    Dim str As String = writer2.ToString
                    Dim num As Integer = 0
                    Dim startIndex As Integer = 0
                    Do While True
                        Dim index As Integer = str.IndexOf(writer2.NewLine, startIndex)
                        If (index = -1) Then
                            Exit Do
                        End If
                        startIndex = (index + writer2.NewLine.Length)
                        num += 1
                    Loop
                    activity.SetValue(ActivityMarkupSerializer.StartLineProperty, num)
                    activity.SetValue(ActivityMarkupSerializer.EndLineProperty, num)
                    activity.SetValue(ActivityMarkupSerializer.EndColumnProperty, startIndex)
                    activity.SetValue(ActivityMarkupSerializer.StartColumnProperty, ((str.IndexOf("<"c, startIndex) - startIndex) + 1))
                End If
                Dim str2 As String = [TryCast](activity.GetValue(WorkflowMarkupSerializer.XClassProperty),String)
                If (Not str2 Is Nothing) Then
                    writer.WriteAttributeString("x", "Class", "http://schemas.microsoft.com/winfx/2006/xaml", str2)
                End If
            End If
        End Sub

        Friend Shared Sub ReplaceChildActivities(ByVal instanceActivity As CompositeActivity, ByVal xomlActivity As CompositeActivity)
            Dim list As New ArrayList
            Dim activity As Activity
            For Each activity In xomlActivity.Activities
                list.Add(activity)
            Next
            Try 
                instanceActivity.CanModifyActivities = True
                xomlActivity.CanModifyActivities = True
                instanceActivity.Activities.Clear
                xomlActivity.Activities.Clear
                Dim activity2 As Activity
                For Each activity2 In list
                    instanceActivity.Activities.Add(activity2)
                Next
            Finally
                instanceActivity.CanModifyActivities = False
                xomlActivity.CanModifyActivities = False
            End Try
            If Not instanceActivity.UserData.Contains(UserDataKeys.CustomActivity) Then
                instanceActivity.UserData.Item(UserDataKeys.CustomActivity) = (instanceActivity.Activities.Count > 0)
            End If
        End Sub


        ' Fields
        Public Shared ReadOnly EndColumnProperty As DependencyProperty = DependencyProperty.RegisterAttached("EndColumn", GetType(Integer), GetType(ActivityMarkupSerializer), New PropertyMetadata(-1, New Attribute() { New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        Public Shared ReadOnly EndLineProperty As DependencyProperty = DependencyProperty.RegisterAttached("EndLine", GetType(Integer), GetType(ActivityMarkupSerializer), New PropertyMetadata(-1, New Attribute() { New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        Private Const minusOne As Integer = -1
        Public Shared ReadOnly StartColumnProperty As DependencyProperty = DependencyProperty.RegisterAttached("StartColumn", GetType(Integer), GetType(ActivityMarkupSerializer), New PropertyMetadata(-1, New Attribute() { New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
        Public Shared ReadOnly StartLineProperty As DependencyProperty = DependencyProperty.RegisterAttached("StartLine", GetType(Integer), GetType(ActivityMarkupSerializer), New PropertyMetadata(-1, New Attribute() { New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
    End Class
End Namespace

